package com.MSofLectures.Entity.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.MSofLectures.DAO.Lecture;
import com.MSofLectures.DAO.Lecture_registerCode;
import com.MSofLectures.DAO.User_keyName;
import com.MSofLectures.DAO.User_keyValue;
import com.MSofLectures.DAO.User_registerLecture;
import com.MSofLectures.Entity.Basic;

public class User extends Basic implements UserImp {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title User.java
	 * @Package com.Entity.User
	 * @author ufo
	 */
	private String userId;
	private String userPwd;
	

	public void altPwd(String newpwd) {
		// com.DAO.User user=(com.DAO.User)
		com.MSofLectures.DAO.User user = (com.MSofLectures.DAO.User) this.getDatabaseControler().load(
				com.MSofLectures.DAO.User.class, userId);
		user.setUserPwd(newpwd);
		this.getDatabaseControler().hibernateSess.save(user);
		this.getDatabaseControler().commit();

	}

	@Override
	public boolean map() {
		// TODO 自动生成的方法存根
		try {
			List<?> list = this.getDatabaseControler().hibernateSess
					.createQuery("from User_keyValue where userId= :userId")
					.setParameter("userId", this.userId).list();
			Iterator<?> i = list.iterator();
			while (i.hasNext()) {
				User_keyValue keyValue = (User_keyValue) i.next();
				String[] tmp = { keyValue.getUser_keyValue(),
						keyValue.getUser_keyValueId() };
				keyValueMap.put(mapKeyName(keyValue.getUser_keyNameId()), tmp);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private String mapKeyName(String keyNameId) {
		User_keyName keyName = (User_keyName) this.getDatabaseControler().hibernateSess
				.load(User_keyName.class, keyNameId);
		return keyName.getUser_keyName();
	}

	@Override
	public void save() {
		// TODO 自动生成的方法存根
		if (0 < keyValueMap.size()) {
			Set<String> set = keyValueMap.keySet();

			try {
				for (Iterator<String> i = set.iterator(); i.hasNext();) {
					String keyName = i.next();
					List<?> list1 = this.getDatabaseControler().hibernateSess
							.createQuery(
									"select user_keyNameId from User_keyName where user_keyName = :user_keyName")
							.setString("user_keyName", keyName).list();
					Iterator<?> i1 = list1.iterator();
					saveKeyValue(i1.next().toString(), keyValueMap.get(keyName));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void saveKeyValue(String keyNameId, String[] target) {
		User_keyValue keyValue = null;
		if (null == target[1]) {
			keyValue = new User_keyValue();
		} else {
			keyValue = (User_keyValue) this.getDatabaseControler().hibernateSess.load(
					User_keyValue.class, target[1]);
		}
		keyValue.setUser_keyValue(target[0]);
		keyValue.setUser_keyNameId(keyNameId);
		keyValue.setUserId(this.userId);
		this.getDatabaseControler().hibernateSess.save(keyValue);

		this.getDatabaseControler().commit();
	}

	@Override
	public boolean isExist() {
		// TODO 自动生成的方法存根
		return null != this.userPwd;
	}

	@Override
	public boolean checkLectureCode(String lecture_registerCode,
			String lectureId) {
		// TODO 自动生成的方法存根
		com.MSofLectures.Entity.Lecture.LectureImp lecture = this.getOperatorOnLecture()
				.loadLecture(lectureId);
		List<?> list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"select user_registerLectureId from User_registerLecture where userId=:userId and lectureId=:lectureId and user_registerLecture_checked=:user_registerLecture_checked and lecture_registerCodeId=null")
				.setString("userId", this.userId)
				.setByte("user_registerLecture_checked",
						Byte.parseByte(lecture.getValue("shake")))// 需要检查是否需要摇号
				// 如果还没有完成摇号，先用这个——(byte)
				// 0,
				.setString("lectureId", lectureId).list();
		Iterator<?> i = list.iterator();
		if (i.hasNext()) {
			com.MSofLectures.DAO.User_registerLecture register = (User_registerLecture) this.getDatabaseControler()
					.load(User_registerLecture.class, i.next().toString());
			List<?> list1 = this.getDatabaseControler().hibernateSess
					.createQuery(
							"from Lecture_registerCode where lecture_registerCode=:registerCode and lectureId=:lectureId and lecture_registerCode_checked=0")
					.setString("registerCode", lecture_registerCode)
					.setString("lectureId", lectureId).list();
			Iterator<?> i1 = list1.iterator();
			if (i1.hasNext()) {
				com.MSofLectures.DAO.Lecture_registerCode registerCode = (Lecture_registerCode) i1
						.next();

				register.setUser_registerLecture_checked((byte) 1);
				register.setLecture_registerCodeId(registerCode
						.getLecture_registerCodeId());
				registerCode.setLecture_registerCode_checked((byte) 1);
				this.getDatabaseControler().save(registerCode);
				this.getDatabaseControler().save(register);
				this.getDatabaseControler().commit();
				return true;
			}
		}
		return false;
	}

	@Override
	public String registerLecture(String lectureId) {
		// 报名参加摇号，这里没有进行摇号的程序，具体程序需要在com.listener中写
		// TODO 自动生成的方法存根
		List<?> list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"from User_registerLecture where userId=:userId and lectureId=:lectureId")
				.setString("userId", this.userId)
				.setString("lectureId", lectureId).list();// 搜索是否已经报名了讲座
		if (list.iterator().hasNext())
			return null;
		else {
			com.MSofLectures.DAO.User_registerLecture register = new com.MSofLectures.DAO.User_registerLecture();
			register.setLectureId(lectureId);
			register.setUserId(this.userId);
			register.setUser_registerLecture_checked((byte) 0);
			this.getDatabaseControler().save(register);
			this.getDatabaseControler().commit();
			return register.getUser_registerLectureId();
		}
	}

	@Override
	public String teacherRegisterLecture(String userId,String lectureId){
		
		List<?> list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"from User_registerLecture where userId=:userId and lectureId=:lectureId")
				.setString("userId", userId)
				.setString("lectureId", lectureId).list();// 搜索是否已经报名了讲座
		Iterator<?> i=list.iterator();
		if (i.hasNext()){
			System.out.println(userId);
			com.MSofLectures.DAO.User_registerLecture register= (User_registerLecture) i.next();
			if((byte)1!=register.getUser_registerLecture_checked()){
				register.setUser_registerLecture_checked((byte) 1);
				register.setUser_registerLecture_checkedOrder("999");
				this.getDatabaseControler().save(register);
				this.getDatabaseControler().commit();
			}
			return register.getUser_registerLectureId();
		}else{
			com.MSofLectures.DAO.User_registerLecture register = new com.MSofLectures.DAO.User_registerLecture();
			register.setLectureId(lectureId);
			register.setUserId(userId);
			register.setUser_registerLecture_checked((byte) 1);
			register.setUser_registerLecture_checkedOrder("999");
			this.getDatabaseControler().save(register);
			this.getDatabaseControler().commit();
			return register.getUser_registerLectureId();
		}
	}
	@Override
	public String userLectureStatus(String lectureId) {
		List<?> list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"select user_registerLectureId from User_registerLecture where userId=:userId and lectureId=:lectureId and user_registerLecture_checked=1 and lecture_registerCodeId is not null")
				.setString("userId", this.userId)
				.setString("lectureId", lectureId).list();// 搜索是否已经签到
		if (list.iterator().hasNext())
			return "check";
		else {
			List<?> list2 = this.getDatabaseControler().hibernateSess
					.createQuery(
							"select user_registerLectureId from User_registerLecture where userId=:userId and lectureId=:lectureId and user_registerLecture_checked=1 ")
					.setString("userId", this.userId)
					.setString("lectureId", lectureId).list();// 搜索是否已经被摇中
			if (list2.iterator().hasNext())

				return "shake";
			else {
				List<?> list3 = this.getDatabaseControler().hibernateSess
						.createQuery(
								"select user_registerLectureId from User_registerLecture where userId=:userId and lectureId=:lectureId and user_registerLecture_checked=0")
						.setString("userId", this.userId)
						.setString("lectureId", lectureId).list();
				if (list3.iterator().hasNext())

					return "noshake";
				else {

					return "noregister";
				}
			}
		}
	}

	@Override
	public String userOrder(String lectureId, String userId) {
		List<?> list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"select user_registerLecture_checkedOrder from User_registerLecture where userId=:userId and lectureId=:lectureId and user_registerLecture_checked=1")
				.setString("userId", userId).setString("lectureId", lectureId)
				.list();//
		Iterator<?> i = list.iterator();
		if (list.iterator().hasNext())
			return i.next().toString();
		else
			return "noorder";
	}

	@Override
	public Map<String, String> showOneRegisterLecture(String registerLectureId) {
		// TODO 自动生成的方法存根
		Map<String, String> map = new HashMap<String, String>();
		com.MSofLectures.DAO.User_registerLecture register = (User_registerLecture) this.getDatabaseControler()
				.load(com.MSofLectures.DAO.User_registerLecture.class, registerLectureId);
		map.put("user_registerLectureId", register.getUser_registerLectureId());
		map.put("registerLecture_checked", 0 != register
				.getUser_registerLecture_checked() ? "true" : "false");
		map.put("userId", register.getUserId());
		map.put("lectureId", register.getLectureId());
		map.put("lecture_registerCodeId", register.getLecture_registerCodeId());
		return map;
	}

	@Override
	public List<String> showAllRegisterLecture() {
		// 展示所有曾经成功过的讲座（不管去没去,或者报名成功与否）
		// TODO 自动生成的方法存根
		List list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"select lectureId from User_registerLecture where userId=:userId")
				.setString("userId", this.userId).list();
		return list;
	}

	@Override
	public List<String> showAllCheckLecture() {
		// 展示所有曾经成功过的讲座（不管去没去,或者报名成功与否）
		// TODO 自动生成的方法存根
		List list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"select lectureId from User_registerLecture where userId=:userId and user_registerLecture_checked=1 and lecture_registerCodeId is not null")
				.setString("userId", this.userId).list();
		return list;
	}

	@Override
	public List<String> viewLectureShakeResult(String lectureId, String shake) {
		// TODO 自动生成的方法存根
		com.MSofLectures.DAO.Lecture lecture = (Lecture) this.getDatabaseControler().load(
				com.MSofLectures.DAO.Lecture.class, lectureId);
		if (!lecture.getLecture_submittorId().equals(this.userId))
			return null;

		List list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"select userId from User_registerLecture where lectureId=:lectureId and user_registerLecture_checked=1 order by user_registerLecture_checkedOrder desc")
				.setString("lectureId", lectureId)
				.list();
		return list;
	}

	@Override
	public List<String> showLectureCode(String lectureId) {
		List list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"select lecture_registerCode from Lecture_registerCode where lectureId=:lectureId")
				.setString("lectureId", lectureId).list();
		return list;
	}

	@Override
	public List<String> showCheck(String lectureId) {
		List list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"select userId from User_registerLecture where lectureId=:lectureId and user_registerLecture_checked=1 and lecture_registerCodeId is not null ")
				.setString("lectureId", lectureId).list();
		return list;
	}

	@Override
	public List<String> listAllUsers() {
		List list = null;
		if (null != this.getValue("privilegeToCreateLecture"))
			if (this.getValue("privilegeToCreateLecture").equals("1"))
				list = this.getDatabaseControler().hibernateSess.createQuery(
						"select userId from User  order by userId").list();
		return list;
	}

	public User(String userId, String userPwd) {
		setUserId(userId);
		setUserPwd(userPwd);
	}

	public User(String userId) {
		setUserId(userId);

	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	@Override
	public List<String> listTheSameYearUsers(String enterYear) {
		// TODO Auto-generated method stub
		List list = null;
		List<String> result = new ArrayList<String>();
		if (null != this.getValue("privilegeToCreateLecture"))
			if (this.getValue("privilegeToCreateLecture").equals("1")) {
				list = this.getDatabaseControler().hibernateSess.createQuery(
						"select userId from User order by userId").list();
				Iterator i = list.iterator();
				while (i.hasNext()) {
					UserImp userTmp=this.getOperatorOnUser().loadUid(i
							.next().toString());
					if(null!=userTmp)
						if (userTmp.getValue("enterYear").equals(enterYear))
							result.add(userTmp.getUserId());
				}
			}
		return result;
	}

	@Override
	public List<String> listTheSameMajorUsers(String major,String enterYear) {
		// TODO Auto-generated method stub
		List<?> list = null;
		List<String> result = new ArrayList<String>();
		if (null != this.getValue("privilegeToCreateLecture"))
			if (this.getValue("privilegeToCreateLecture").equals("1")) {
				list = this.getDatabaseControler().hibernateSess.createQuery(
						"select userId from User order by userId").list();
				Iterator<?> i = list.iterator();
				while (i.hasNext()) {
					UserImp userTmp=this.getOperatorOnUser().loadUid(i
							.next().toString());
					if(null!=userTmp)
						if (userTmp.getValue("enterYear").equals(enterYear)&userTmp.getValue("major").equals(major))
							result.add(userTmp.getUserId());
				}
			}
		return result;
	}

	@Override
	public boolean deleteUser_registerLecture(String lectureId) {
		// TODO 自动生成的方法存根
		try{
			User_registerLecture lecture=(User_registerLecture) this.getDatabaseControler().hibernateSess
					.createQuery("from User_registerLecture where lectureId=:lectureId and userId=:userId")
					.setString("lectureId", lectureId)
					.setString("userId", this.userId)
					.list().iterator().next();
				this.getDatabaseControler().delete(lecture);
				getDatabaseControler().commit();
				return true;
			
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
}
